#include "general_my.h"


#define MAXIMUM_LENGTH 200

typedef struct test_pair
{
    int backpack_volume;
    int * item_volume;
    struct test_pair * next;
    struct test_pair * previous;
} test_pair;

typedef struct test_chain
{
    int length;
    test_pair * head;
    test_pair * end;
    test_pair * temp;
} test_chain;


test_chain * create_chain(){
    test_chain * chain = (test_chain *) malloc (sizeof(test_chain));
    if (NULL == chain){
        printf("Memory allocate error, exiting\n");
        exit(-1);
    }
    
    test_pair * head_pair = (test_pair *) malloc( sizeof(test_pair));
    if (NULL == head_pair){
        printf("Memory allocate error, exiting\n");
        exit(-1);
    }
    
    test_pair * end_pair = (test_pair *) malloc(sizeof(test_pair));
    if (NULL == end_pair){
        printf("Memory allocate error, exiting\n");
        exit(-1);
    }
    
    test_pair * temp_pair = (test_pair *) malloc(sizeof(test_pair));
    if (NULL == temp_pair){
        printf("Memory allocate error, exiting\n");
        exit(-1);
    }
    
    chain->head = head_pair;
    chain->end = end_pair;
    chain->head->next = chain->end;
    chain->end->next = NULL;
    chain->length = 0;
    chain->temp = chain->head;

    return chain;
}

int append(test_chain * chain, test_pair * pair){
    pair->next = chain->temp->next;
    chain->temp->next = pair;
    chain->temp = pair;
    chain->length += 1;
    return 0;
}


int test_linked_list(){
    test_chain * chain = (test_chain *) create_chain();
    test_pair * pair;
    for (int i=0; i<10; i++){
        pair = (test_pair *) malloc(sizeof(test_pair));
        pair->backpack_volume = i;
        // 复合字面量，注意字面量是在静态区的，因此最后所有pair的都指向{9,2,3}
        pair->item_volume = (int []){i, 2, 3};
        append(chain, pair);
    }
    printf("Done!\n");
    return 0;
}

int * str2numlist(char * str){
    int numNum = 1;
    for (int i=0; i< strlen(str); i++)
        if (*(str+i) == ' ')
            numNum+=1;

    int temp_num = 0;
    int * num_list = (int *) malloc(sizeof(int) * (numNum + 1));
    // =* 初始化不定长数组，每个元素置为0，第一个元素设置为长度
    memset(num_list, 0, numNum);
    *(num_list + 0) = numNum + 1;

    for (int i=0, j=1; i<=strlen(str);i++){
        // * 数字以空格分割
        if (*(str + i) == ' '){
            *(num_list + j) = temp_num;
            j++;
            temp_num = 0;
        }

        // * 直接的字符数组末尾是\0
        if (*(str+i) == '\0'){
            *(num_list + j) = temp_num;
            j++;
            temp_num = 0;
        }

        // * fgets一次性读取一行，不会去除\n
        if (*(str + i) == '\n'){
            *(num_list + j) = temp_num;
            j++;
            return num_list;
        }

        if (*(str+i)-'0' < 10 and *(str+i)-'0' >=0)
            temp_num = temp_num * 10 + *(str+i) - '0';
    }
    return num_list;
}

int test_str2numlist(){
    // char  s[20] = "1 12 23 12 0";
    char * s = "12";
    printf("%s\n", s);

    int * num_list = str2numlist(s);

    for (int i=1 ; i<*(num_list+0); i++)
        printf("%d: %d\n", i, *(num_list+ i));
    return 0;
}

test_chain * read_file(char * filename, char * mode){
    FILE * fp;
    // 使用 NULL == pointer 避免打掉一个=导致给pointer赋值
    if (NULL == (fp = fopen(filename, mode))){
        // == 使用perror打印错误信息，该信息保存在errno中，由系统维护
        perror("File open error, exiting...\n");
        exit(-1);
    }
    
    char * buffer = (char *) malloc(sizeof(char) * MAXIMUM_LENGTH);
    if (NULL == buffer){
        printf("Memory allocate error, exiting...\n");
        exit(-1);
    }

    int i = 0;
    test_chain * chain = (test_chain *) create_chain();
    test_pair * pair = (test_pair *) malloc(sizeof(test_pair));
    while (NULL != fgets(buffer, MAXIMUM_LENGTH, fp)){
        printf("%s\n", buffer);
        if (i%2 == 0){
            if (i != 0)
                append(chain, pair);
            pair = (test_pair *) malloc(sizeof(test_pair));
            pair->backpack_volume = *((int *)str2numlist(buffer)+1);
            i++;
            continue;
        }
        pair->item_volume = (int *) str2numlist(buffer);
        i++;
    }
    append(chain, pair);
    fclose(fp);
    return chain;
}


int main(int argc, char *argv[]){
    // test_linked_list();
    // test_str2numlist();
    // == 注意要写成相对可执行文件的路径
    read_file("../test_file.txt", "r");
    return 0;
}